package hero.mps.web.school_teacher.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hero.mps.web.school_teacher.entity.SchoolTeacher;
import hero.mps.web.school_teacher.mapper.SchoolTeacherMapper;
import hero.mps.web.school_teacher.service.SchoolTeacherService;
import hero.mps.web.stu_points.entity.StuPoints;
import hero.mps.web.stu_points.service.StuPointsService;
import hero.mps.web.teacher_role.entity.TeacherRole;
import hero.mps.web.teacher_role.service.TeacherRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Wrapper;
import java.util.List;

@Service
@RequiredArgsConstructor
public class SchoolTeacherServiceImpl extends ServiceImpl<SchoolTeacherMapper, SchoolTeacher> implements SchoolTeacherService {
    private final TeacherRoleService teacherRoleService;
    private final StuPointsService stuPointsService;

    @Override
    @Transactional
    public void addTeacher(SchoolTeacher schoolTeacher) {
        int insert = this.baseMapper.insert(schoolTeacher);
        if (insert > 0) {
//            设置教师角色
            TeacherRole teacherRole = new TeacherRole();
            teacherRole.setTeacherId(schoolTeacher.getTeacherId());
            teacherRole.setRoleId(schoolTeacher.getRoleId());
            teacherRoleService.save(teacherRole);
        }
    }

    @Override
    @Transactional
    public void editTeacher(SchoolTeacher schoolTeacher) {
        int updatedById = this.baseMapper.updateById(schoolTeacher);
        if (updatedById > 0) {
//            先删除
            LambdaQueryWrapper<TeacherRole> queryWrapper = Wrappers.lambdaQuery(TeacherRole.class)
                    .eq(TeacherRole::getTeacherId, schoolTeacher.getTeacherId());
            teacherRoleService.remove(queryWrapper);
//            在保存
            TeacherRole teacherRole = new TeacherRole();
            teacherRole.setTeacherId(schoolTeacher.getTeacherId());
            teacherRole.setRoleId(schoolTeacher.getRoleId());
            teacherRoleService.save(teacherRole);
        }
    }

    @Override
    public void deleteTeacher(Integer teacherId) {
        LambdaQueryWrapper<TeacherRole> queryWrapper = Wrappers.lambdaQuery(TeacherRole.class)
                .eq(TeacherRole::getTeacherId, teacherId);
        boolean remove = teacherRoleService.remove(queryWrapper);
        if (remove) {
            this.baseMapper.deleteById(teacherId);
        }
    }

    @Override
    @Transactional
    public void saveStuPoints(List<StuPoints> stuPoints, Integer classId, Integer courseId) {
        //先删除
        LambdaQueryWrapper<StuPoints> queryWrapper = Wrappers.lambdaQuery(StuPoints.class)
                .eq(StuPoints::getCourseId, courseId)
                .eq(StuPoints::getClassId, classId);
        stuPointsService.remove(queryWrapper);
        //再保存
        stuPointsService.saveBatch(stuPoints);
    }
}
